﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class PhieuNo_DAO
    {
        private static PhieuNo_DAO m_instance = null;

        public static PhieuNo_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new PhieuNo_DAO();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy mã phiếu nợ lớn nhất
        /// </summary>
        /// <returns></returns>
        public DataTable LayMaPhieuNoMax()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("GET_MAPHIEUNO_MAX", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy thông tin của phiếu nợ cuối cùng của 1 phiếu bán hàng
        /// </summary>
        /// <param name="mapbh"></param>
        /// <returns></returns>
        public DataTable LayPhieuNoCuoi(string mapbh)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_PHIEUNO_CUOI_BY_PHIEUBANHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphieu", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maphieu"].Value = mapbh;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Thêm phiếu nợ mới
        /// </summary>
        /// <returns></returns>
        public bool ThemPhieuNoMoi(string mapbh, string ngNo, long tienNo, string ngTra, long tienTra, long conLai)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_PHIEUNO_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphieubanhang", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maphieubanhang"].Value = mapbh;

                cmd.Parameters.Add("@ngayno", SqlDbType.SmallDateTime);
                cmd.Parameters["@ngayno"].Value = ngNo;

                cmd.Parameters.Add("@sotientrano", SqlDbType.Money);
                cmd.Parameters["@sotientrano"].Value = tienNo;

                cmd.Parameters.Add("@ngaytra", SqlDbType.SmallDateTime);
                cmd.Parameters["@ngaytra"].Value = ngTra;

                cmd.Parameters.Add("@sotientra", SqlDbType.Money);
                cmd.Parameters["@sotientra"].Value = tienTra;

                cmd.Parameters.Add("@conlai", SqlDbType.Money);
                cmd.Parameters["@conlai"].Value = conLai;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Lấy toàn bộ phiếu nợ của phiếu bán hàng
        /// </summary>
        /// <param name="mapbh"></param>
        /// <returns></returns>
        public DataTable LayToanBoPhieuNo(string mapbh)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYPHIEUNOBYMAPHIEUBAN", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphieubanhang", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maphieubanhang"].Value = mapbh;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
